﻿using System.ComponentModel;
using BunnyEater.Common.ResponseOutput.Enum;
using BunnyEater.Common.Extensions;

namespace BunnyEater.Common.ResponseOutput
{
    /// <summary>
    /// 响应返回的实体
    /// </summary>
    /// <typeparam name="T">响应体</typeparam>
    public class ResponseReturnsEntity<T>
    {
        [Description("消息")]
        public string? Message { get; set; }

        [Description("响应码")]
        public ResponseCodeEnum ResponseCode { get; set; }

        [Description("数据对象")]
        public T? Data { get; set; }

        [Description("数据总记录条数")]
        public int TotalCount { get; set; }

        [Description("刷新令牌")]
        public string? RefreshToken { get; set; }

        [Description("账号令牌")]
        public string? AccountToken { get; set; }

        /// <summary>
        /// 响应成功
        /// </summary>
        /// <param name="data">返回的数据对象</param>
        /// <param name="totalCount">数据对象的总记录条数</param>
        /// <param name="message">返回响应成功的消息</param>
        /// <returns>返回响应实体</returns>
        public static ResponseReturnsEntity<T> Success(T data, string accountToken = "", string refreshToken = "", string message = "", int totalCount = 1)
        {
            var result = new ResponseReturnsEntity<T>()
            {
                Message = string.IsNullOrEmpty(message) ? ResponseCodeEnum.Success.GetDescription() : message,
                ResponseCode = ResponseCodeEnum.Success,
                Data = data,
                TotalCount = totalCount,
                AccountToken = accountToken,
                RefreshToken = refreshToken
            };
            return result;
        }

        /// <summary>
        /// 响应失败，有数据对象返回
        /// </summary>
        /// <param name="data">响应返回的对象</param>
        /// <returns>返回响应实体</returns>
        public static ResponseReturnsEntity<T> Fail(T data)
        {
            var result = new ResponseReturnsEntity<T>()
            {
                Message = ResponseCodeEnum.Fail.GetDescription(),
                ResponseCode = ResponseCodeEnum.Fail,
                Data = data
            };
            return result;
        }

        /// <summary>
        /// 响应失败，无数据对象返回
        /// </summary>
        /// <param name="message">响应返回的信息</param>
        /// <returns>返回响应实体</returns>
        public static ResponseReturnsEntity<T> Fail(string message = "")
        {
            var result = new ResponseReturnsEntity<T>()
            {
                Message = string.IsNullOrEmpty(message) ? ResponseCodeEnum.Fail.GetDescription() : message,
                ResponseCode = ResponseCodeEnum.Fail,
            };
            return result;
        }

        /// <summary>
        /// 响应后系统错误，请联系管理员
        /// </summary>
        /// <param name="message">响应返回的信息</param>
        /// <returns>返回响应实体</returns>
        public static ResponseReturnsEntity<T> SystemError(string message = "")
        {
            var result = new ResponseReturnsEntity<T>()
            {
                Message = string.IsNullOrEmpty(message) ? ResponseCodeEnum.SystemError.GetDescription() : message,
                ResponseCode = ResponseCodeEnum.SystemError,
            };
            return result;
        }
    }
}
